package cn.cool.controller;


import cn.cool.dto.UserRegisterDTO;
import cn.cool.entity.User;
import cn.cool.service.UserService;
import cn.cool.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.WebUtils;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiuH
 * @since 2021-01-23
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private VerificationCode verificationCode;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "根据登录账号和密码登录")
    public Object login(User user) {
        Object userInfo = userService.login(user);
        return userInfo;
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public Object register(UserRegisterDTO user) {
        Object registerInfo = userService.register(user);
        return registerInfo;
    }

    @GetMapping("/logout")
    @ApiOperation(value = "退出登录")
    public Object logOut(HttpServletRequest request) {
        return userService.logOut(request);
    }

    @GetMapping("/getRegisterCode")
    @ApiOperation(value = "获取验证码")
    public void registerCode(HttpServletResponse response) throws IOException{
        //获取验证码图片
        BufferedImage image = verificationCode.getImage();
        //获取验证码内容
        String text = verificationCode.getText();
        //randomCode用于保存验证码 以便用户登录后验证
        StringBuffer randomCode = new StringBuffer();
        randomCode.append(text);
        //将验证码放入 list 缓存,有效期1分钟
        redisUtil.lSet("randomCode",randomCode.toString(),60);
        //禁止图像缓存
        response.setHeader("Pragma","no-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires",0);
        response.setContentType("image/jpeg");
        //将图像输出到Servlet输出流中
        ServletOutputStream sos = response.getOutputStream();
        ImageIO.write(image,"jpeg",sos);
        sos.flush();
        sos.close();
    }

    @GetMapping("/userInfo")
    @ApiOperation(value = "查看当前用户信息")
    public Object userInfo(HttpServletRequest request){
        Integer userId = JWTUtils.getUserId(request);
        //获取当前用户
        User user = userService.getById(userId);
        //不携带密码到前端
        user.setUserPassword("0");
        return user;
    }

    @PostMapping("/setUserInfo")
    @ApiOperation(value = "设置用户信息")
    public Object setUserInfo(HttpServletRequest request,User user,@RequestParam(required = false) MultipartFile file){
        Object result = userService.setUserInfo(request, user, file);
        return result;
    }

}

